package br.com.dev.tcc.task;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.bean.DelegateListener;
import br.com.dev.tcc.bean.ResponseData;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.connection.SinistroRetorno;
import br.com.dev.tcc.connection.WSClient;
import br.com.dev.tcc.util.SinistroParser;

public class BuscaSinistrosTask extends
		AsyncTask<Void, Void, ResponseData<List<Sinistro>>> {

	private Apolice apolice;
	private DelegateListener<ResponseData<List<Sinistro>>> delegate;
	private Context context;

	public BuscaSinistrosTask(Context context,
			DelegateListener<ResponseData<List<Sinistro>>> delegate,
			Apolice apolice) {
		this.context = context;
		this.delegate = delegate;
		this.apolice = apolice;
	}

	@Override
	protected ResponseData<List<Sinistro>> doInBackground(Void... params) {

		WSClient client = new WSClient();

		try {
			ArrayList<SinistroRetorno> buscarDadosSinistro = client
					.buscarDadosSinistro(context, apolice.getId());
			List<Sinistro> parse = SinistroParser.parse(buscarDadosSinistro);
			return new ResponseData<List<Sinistro>>(true, null, parse);
		} catch (Exception e) {
			return new ResponseData<List<Sinistro>>(false, e.getMessage(), null);
		}
	}

	@Override
	protected void onPostExecute(ResponseData<List<Sinistro>> result) {
		delegate.onDelegate(result);
	}
}
